package com.jingxuan.util;

import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;

//利用google的libphonenumber验证手机号
public class CheckValidPhone {
    public static boolean isValidPhone(String phone){
        if(phone == null || phone.isEmpty()){
            return false;
        }
        PhoneNumberUtil util=PhoneNumberUtil.getInstance();
        try{
            Phonenumber.PhoneNumber phoneNumber=util.parse(phone,"CN");
            if(!util.isValidNumberForRegion(phoneNumber, "CN")) return false;
            PhoneNumberUtil.PhoneNumberType type=util.getNumberType(phoneNumber);
            return type==PhoneNumberUtil.PhoneNumberType.MOBILE
            ||type==PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE;
        } catch (NumberParseException e) {
            return false;
        }
    }
}
